package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.entity.penum.IotypeEnum;
import com.qianniu.napi.admin.entity.penum.XftypeEnum;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 交易流水表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-07
 */
@RestController
public class AccountFlowController {

    private static final Logger logger = LoggerFactory.getLogger(AccountFlowController.class);

    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountFlowService accountflowService;
    @Autowired
    private IAccountTixianService accountTixianService;

    @Autowired
    private IMaccountService maccountService;
    @Autowired
    private IMaccountFlowService maccountFlowService;

    /// / ************* bellow is bean interface for user *********

    @RequestMapping("/api/accountflow/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "type", required = false) Integer type,
                      @RequestParam(value = "iotype", required = false) Integer iotype,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo json = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Long uid = ObjectUtil.getUid();
        Page page = new Page(pageIndex, pageSize);

        Wrapper<AccountFlow> entity = new EntityWrapper<AccountFlow>()
                .eq("uid", uid)
                .eq("is_confirm", 1);

        if (type != null) {
            page = accountflowService.selectPage(page, entity.eq("xftype", type));
        }
        if (iotype != null) {
            if (iotype != 0) entity.eq("iotype", iotype);
        }
//        if(ts==1){
//            page = accountflowService.selectPage(page, entity
//                    .gt("tradetime",LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
//                    .lt("tradetime",LocalDateTime.now().withNano(0).withHour(23).withMinute(59).withSecond(59).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
//        }else if(ts==2){
//            page = accountflowService.selectPage(page, entity
//                    .gt("tradetime",LocalDateTime.now().minusMonths(1).withNano(0).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
//                    .lt("tradetime",LocalDateTime.now().withNano(0).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
//        }else if(ts==3){
//            page = accountflowService.selectPage(page, entity
//                    .gt("tradetime",LocalDateTime.now().minusMonths(3).withNano(0).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
//                    .lt("tradetime",LocalDateTime.now().withNano(0).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
//        }else if(ts==4){
//        }
        entity.orderBy("id", false);
        Page<AccountFlow> data = accountflowService.selectPage(page, entity);

        json.setInfo(data.getRecords());
        return json;
    }

    @RequestMapping("/api/accountflow/czlist")
    @ResponseBody
    public Jinfo czlist(@RequestParam(value = "type", required = false) Integer type,
                        @RequestParam(value = "iotype", required = false) Integer iotype,
                        @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                        @RequestParam(value = "pageSize", required = false) Integer pageSize,
                        HttpServletRequest req) {
        Jinfo json = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Long uid = ObjectUtil.getUid();
        Page page = new Page(pageIndex, pageSize);

        Wrapper<AccountFlow> entity = new EntityWrapper<AccountFlow>()
                .eq("uid", uid)
                .eq("xftype", XftypeEnum.recharge.getVal())
                .eq("is_confirm", 1);

        entity.orderBy("id", false);
        Page<AccountFlow> data = accountflowService.selectPage(page, entity);

        json.setInfo(data.getRecords());
        return json;
    }

    @RequestMapping("/api/accountflow/xflist")
    @ResponseBody
    public Jinfo xflist(@RequestParam(value = "type", required = false) Integer type,
                        @RequestParam(value = "iotype", required = false) Integer iotype,
                        @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                        @RequestParam(value = "pageSize", required = false) Integer pageSize,
                        HttpServletRequest req) {
        Jinfo json = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Long uid = ObjectUtil.getUid();
        Page page = new Page(pageIndex, pageSize);

        List<String> xflist = new ArrayList<String>();
        xflist.add("4");
        xflist.add("7");

        Wrapper<AccountFlow> entity = new EntityWrapper<AccountFlow>()
                .eq("uid", uid)
                .in("xftype", xflist)
                .eq("is_confirm", 1);

        entity.orderBy("id", false);
        Page<AccountFlow> data = accountflowService.selectPage(page, entity);

        json.setInfo(data.getRecords());
        return json;
    }

    @RequestMapping("/api/accountflow/add")
    @ResponseBody
    public JsonDO accountflowAdd(@Valid AccountFlow bean,
                                 BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        bean.setCreatetime(new Date());
        bean.insert();
        return json;
    }

    @RequestMapping("/api/accountflow/modify")
    @ResponseBody
    public JsonDO accountflowUpdate(HttpServletRequest req,
                                    AccountFlow bean) {
        JsonDO json = new JsonDO();
        bean.setModifytime(new Date());
        bean.updateById();
        return json;
    }

    @RequestMapping("/api/accountflow/detail")
    @ResponseBody
    public JsonDO getAccountFlowDetailById(@RequestParam("token") String token,
                                           @RequestParam("id") long id, HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        AccountFlow bean = accountflowService.selectById(id);
        json.setData(bean);
        return json;
    }

    @RequestMapping("/api/accountflow/yajinlog")
    @ResponseBody
    public Jinfo yajinlog() {
        Jinfo json = new Jinfo();

        Wrapper<AccountFlow> wrapper = new EntityWrapper<AccountFlow>().setSqlSelect("id,realpay,xftype,tradetime")
                .eq("uid", ObjectUtil.getUid());
        wrapper.ge("xftype", XftypeEnum.jyajin.getVal()).le("xftype", XftypeEnum.jyajin.getVal()).orderBy("id", true);

        List<AccountFlow> list = accountflowService.selectList(wrapper);

        if (list == null) list = new ArrayList<AccountFlow>();
        AccountFlow af = new AccountFlow();
        af.setId(1l);
        af.setRealpay(new BigDecimal(10));
        af.setXftype(11);
        af.setTradetime(new Date());
        list.add(af);

        AccountFlow af1 = new AccountFlow();
        af1.setId(2l);
        af1.setRealpay(new BigDecimal(30));
        af1.setXftype(12);
        af1.setTradetime(new Date());
        list.add(af1);

        json.setInfo(list);
        return json;
    }

    @RequestMapping("/api/cash/txlist")
    @ResponseBody
    public Jinfo xjtxlist(
            @RequestParam(value = "shenhe", required = false) Integer shenhe,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo json = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Long uid = ObjectUtil.getUid();
        Page page = new Page(pageIndex, pageSize);

        Wrapper<AccountFlow> entity = new EntityWrapper<AccountFlow>()
                .eq("xftype", XftypeEnum.withdraw.getVal())
                .eq("mpid", ObjectUtil.getMpid())
                .eq("iotype", IotypeEnum.output.getVal());

        if (shenhe == null || shenhe == 0) entity.eq("uid", uid);

        entity.orderBy("id", false);
        page = accountflowService.selectPage(page, entity);

        json.setInfo(page.getRecords());
        return json;
    }

    //提现记录
    @RequestMapping("/api/account/txlist")
    @ResponseBody
    public Jinfo accounttxlist(
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "shenhe", required = false) Integer shenhe,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {

        Jinfo json = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Long uid = ObjectUtil.getUid();
        Page page = new Page(pageIndex, pageSize);

        Wrapper<AccountTixian> entity = new EntityWrapper<AccountTixian>()
                .eq("mpid", ObjectUtil.getMpid())
                .orderBy("id", false);

        if (shenhe == null && shenhe == 0) entity.eq("uid", uid);

        if (type != null) entity.eq("txtype", type);

        entity.orderBy("id", false);
        page = accountTixianService.selectPage(page, entity);

        json.setInfo(page.getRecords());
        return json;
    }

}
